using System;
using System.Linq;
using CommandSystem;
using CustomCharacter.Ability;
using Exiled.API.Features;
using RgCore.PlayerHud;

namespace CustomCharacter.Commands
{
    public class AbilityCommand : ICommand
    {
        public string Command => "ability";

        public string[] Aliases => new string[] { "ab" };

        public string Description => "技能命令";

        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            response = "";
            if (!Player.TryGet(sender, out var player) ||
                !AbilityManager.Abilities.TryGetValue(player, out var ability))
            {
                return false;
            }

            var ab = ability.Where(x => x.Ability.Type == AbilityType.Offensive).ToList();
            if (ab.Count > 0)
            {
                int index = int.TryParse(arguments.At(0), out index) ? index : -1;
                if (index < ab.Count)
                {
                    bool result = player.Trigger(ab[index].Ability, out string resp, AbilityTrigger.Active);
                    if (!string.IsNullOrEmpty(resp))
                    {
                        player.SendFormattedMsg("ability", "<color=#008A60>技能</color>", resp, 3,
                            result ? MsgType.Success_Response : MsgType.Failed_Response, ScreenLocation.CenterTop,
                            true);
                    }
                }
            }

            return true;
        }
    }
}